<template>
	<view style="user-select: text;"class="contentFoot">
		<sunMenu></sunMenu>
		<view class="parse-content" style="display: none;">
			<u-parse :content="content" class="parse"></u-parse> 
		</view>
		<!-- 添加商品展示区域，当 type=1 时显示 -->
		<!-- <view v-if="query.noticeType === '1'" class="products-container" style="margin-top: 40rpx;">
			<view class="section-title">商品列表</view>
			<view class="products-grid">
				<view v-for="item in productList" :key="item.id" class="product-card">
					<image :src="item.image" mode="aspectFill" class="product-image"/>
					<view class="product-info">
						<text class="product-name">{{ item.name }}</text>
						<text class="product-price">NT$ {{ getPrice(item) }}</text>
						<select v-if="item.specs && item.specs.length > 1"
							v-model="item.selectedSpec" 
							class="item-spec"
							@change="updatePrice(item)">
							<option v-for="spec in item.specs" 
								:key="spec.name" 
								:value="spec.name">
								{{ spec.name }}
							</option>
						</select>
						<text v-else class="item-spec-text">通用尺寸</text>
					</view>
				</view>
			</view>
		</view> -->
		<view style="height: 50rpx;"></view>
		<view class="footer">
			<text class="footer-text">© 2025 夏璽玥網路有限公司 版權所有 XIA AKOYA INC. All Rights Reserved. 統一編號90370186</text>
			 
		</view>
		<view style="height: 50rpx;"></view>
	</view>
</template>

<script>
	import sunMenu from '../components/sun-menu/sun-menu.vue'
	import { getInformation, getProducts } from '@/api/api.js'
	export default {
		components: {
			sunMenu
		},
		data() {
			return {
				content: '',
				query:{
					noticeType: ''
				},
				productList: [], // 添加商品列表数据
			}
		},
		onLoad(op) { 
			this.getList(op)
		},
		methods: {
			getList(e){
				this.query.noticeType = e.type
				this.query.activityId = e.activityId
				getInformation(this.query).then(res=>{  
					this.content = this.appendToVideoUrl(res.rows[0].noticeContent)
				})
				
				// 当 type=1 时获取商品列表
				if (this.query.noticeType === '1') {
					this.getProducts()
				}
			},
			appendToVideoUrl(content) {
				if (!content) return '';
				const screenWidth = window.innerWidth;
				const isMobile = screenWidth < 768;
				
				// ===================== 处理视频 =====================
				const videoRegex = /(<iframe\s+[^>]*)(>)/g;
				// 移动端：使用rpx单位，宽680rpx
				// PC端：使用px单位，宽700px
				const videoStyle = isMobile 
					? ' style="width: 680rpx;height: 400rpx;"'  // 移动端视频样式
					: ' style="width: 700px;height: 400px;"';   // PC端视频样式
				
				// 为视频添加样式
				content = content.replace(videoRegex, (match, p1, p2) => {
					return `${p1}${videoStyle}${p2}`;
				});
				
				// ===================== 处理图片 =====================
				const imgRegex = /(<img\s+[^>]*)(\/?>)/g;
				// 移动端：最大宽度680rpx，宽度100%自适应
				// PC端：最大宽度700px，宽度100%自适应
				const imgStyle = isMobile
					? ' style="width: 100%;"'  // 移动端图片样式
					: ' style="width: 100%;"';  // PC端图片样式�片样式
					
				// 为图片添加样式
				content = content.replace(imgRegex, (match, p1, p2) => {
					// 如果图片标签已经有style属性
					if (p1.includes('style="')) {
						// 在原有style基础上添加宽度限制
						return p1.replace(
							'style="', 
							`style="max-width: ${isMobile ? '680rpx' : '700px'};width: 100%;`
						) + p2;
					}
					// 如果没有style属性，直接添加新的style
					return `${p1}${imgStyle}${p2}`;
				});
				
				return content;
			},
			// 添加获取商品列表方法
			getProducts() {
				getProducts().then(res => {
					if (res.data && Array.isArray(res.data)) {
						// 过滤并处理商品数据
						this.productList = res.data
							.filter(product => {
								// 检查商品是否属于当前活动
								if (!product.activityId) return false;
								
								const activityIds = product.activityId.split(',')
									.map(id => parseInt(id.trim()));
								
								return activityIds.includes(parseInt(this.query.activityId));
							})
							.map(product => {
								// 处理规格数据
								const specs = typeof product.specs === 'string' ? 
									JSON.parse(product.specs) : product.specs;
								
								// 设置默认选中的规格和价格
								const defaultSpec = specs && specs.length > 0 ? specs[0] : null;
								
								return {
									...product,
									specs: specs,
									selectedSpec: defaultSpec ? defaultSpec.name : null,
									price: defaultSpec ? defaultSpec.price : product.price
								};
							});
					}
				}).catch(err => {
					console.error('Error fetching products:', err);
				});
			},
			getPrice(item) {
				if (!item.specs || !item.specs.length) return item.price;
				// 确保有选中的规格
				if (!item.selectedSpec) {
					item.selectedSpec = item.specs[0].name;
					item.price = item.specs[0].price;
				}
				const spec = item.specs.find(s => s.name === item.selectedSpec);
				return spec ? spec.price : item.price;
			},
			updatePrice(item) {
				const spec = item.specs.find(s => s.name === item.selectedSpec);
				if (spec) {
					item.price = spec.price;
				}
			}
		}
	}
</script>

<style scoped>
	.parse {
		display: flex;
		justify-content: center;
		text-align: center;
		padding: 24rpx;
	}
	
	/* 添加商品展示相关样式 */
	.section-title {
		font-size: 36rpx;
		font-weight: bold;
		color: #333;
		text-align: center;
		margin: 32rpx 0;
		position: relative;
		padding-bottom: 16rpx;
	}
	
	.section-title::after {
		content: '';
		position: absolute;
		bottom: 0;
		left: 50%;
		transform: translateX(-50%);
		width: 60rpx;
		height: 4rpx;
		background-color: #ff4d4f;
		border-radius: 2rpx;
	}

	.products-container {
		padding: 32rpx;
		max-width: 1600rpx;
		margin: 0 auto;
		margin-bottom: 60rpx;
		background-color: #f8f9fa;
		border-radius: 16rpx;
	}

	.products-grid {
		display: grid;
		grid-template-columns: repeat(auto-fill, minmax(360rpx, 1fr));
		gap: 40rpx;
		padding: 24rpx;
	}

	.product-card {
		background: #ffffff;
		border-radius: 16rpx;
		overflow: hidden;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.08);
		transition: all 0.3s ease;
		border: 1px solid #eee;
	}

	.product-card:hover {
		transform: translateY(-6rpx);
		box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.12);
	}

	.product-image {
		width: 100%;
		height: 360rpx;
		object-fit: cover;
		transition: transform 0.3s ease;
	}

	.product-card:hover .product-image {
		transform: scale(1.05);
	}

	.product-info {
		padding: 24rpx;
		display: flex;
		flex-direction: column;
		gap: 12rpx;
		background: #fff;
	}

	.product-name {
		font-size: 28rpx;
		font-weight: 600;
		color: #333;
		line-height: 1.4;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
	}

	.product-price {
		font-size: 32rpx;
		color: #ff4d4f;
		font-weight: bold;
		margin: 8rpx 0;
	}

	.item-spec {
		width: 100%;
		padding: 12rpx;
		border: 1px solid #ddd;
		border-radius: 8rpx;
		font-size: 26rpx;
		background-color: #fff;
		color: #333;
		cursor: pointer;
		transition: all 0.3s ease;
	}

	.item-spec:hover {
		border-color: #ff4d4f;
	}

	.item-spec:focus {
		outline: none;
		border-color: #ff4d4f;
		box-shadow: 0 0 0 2px rgba(255, 77, 79, 0.1);
	}

	.item-spec-text {
		display: block;
		padding: 12rpx;
		font-size: 26rpx;
		color: #666;
		background-color: #f5f5f5;
		border-radius: 8rpx;
		text-align: center;
	}

	@media screen and (max-width: 768px) {
		.products-container {
			padding: 24rpx;
			margin: 0 24rpx 40rpx 24rpx;
			width: auto;
		}

		.products-grid {
			grid-template-columns: repeat(2, 1fr);
			gap: 20rpx;
			padding: 16rpx;
		}
		
		.product-image {
			height: 280rpx;
		}
		
		.product-name {
			font-size: 26rpx;
		}
		
		.product-price {
			font-size: 30rpx;
		}
		
		.item-spec, .item-spec-text {
			font-size: 24rpx;
			padding: 10rpx;
		}

		.section-title {
			font-size: 32rpx;
			margin: 24rpx 0;
		}
	}
</style>